import 'package:amoy_ticket/utils/smart_dialog/custom_loading.dart';
import 'package:amoy_ticket/utils/smart_dialog/custom_toast.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class UseSmartDialog extends StatefulWidget {
  UseSmartDialog({Key? key}) : super(key: key);

  @override
  _UseSmartDialogState createState() => _UseSmartDialogState();
}

class _UseSmartDialogState extends State<UseSmartDialog> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('use smart dialog'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Text('Toast', textScaleFactor: 2),
            Wrap(
              spacing: 20,
              alignment: WrapAlignment.center,
              runSpacing: 20,
              runAlignment: WrapAlignment.center,
              children: [
                ElevatedButton(
                    onPressed: () {
                      SmartDialog.showToast('test toast');
                    },
                    child: Text('default')),
                ElevatedButton(onPressed: () {}, child: Text('location')),
                ElevatedButton(
                    onPressed: () {
                      SmartDialog.compatible
                          .showToast('', widget: CustomToast('custom toast'));
                    },
                    child: Text('custom')),
                ElevatedButton(onPressed: () {}, child: Text('smart')),
              ],
            ),
            Text('Loading', textScaleFactor: 2),
            Wrap(
              spacing: 20,
              alignment: WrapAlignment.center,
              runSpacing: 20,
              runAlignment: WrapAlignment.center,
              children: [
                ElevatedButton(
                    onPressed: () async {
                      SmartDialog.showLoading();
                      await Future.delayed(Duration(seconds: 2));
                      SmartDialog.dismiss();
                    },
                    child: Text('default')),
                ElevatedButton(
                    onPressed: () async {
                      var maskWidget = Container(
                        width: double.infinity,
                        height: double.infinity,
                        child: Opacity(
                          opacity: 0.6,
                          child: CachedNetworkImage(
                            imageUrl:
                                'https://cdn.jsdelivr.net/gh/xdd666t/MyData@master/pic/flutter/blog/20211101103911.jpeg',
                            fit: BoxFit.fill,
                          ),
                        ),
                      );
                      SmartDialog.compatible
                          .showLoading(maskWidgetTemp: maskWidget);
                      await Future.delayed(Duration(seconds: 2));
                      SmartDialog.dismiss();
                    },
                    child: Text('param')),
                ElevatedButton(
                    onPressed: () async {
                      SmartDialog.compatible.showLoading(
                          isLoadingTemp: false, widget: CustomLoading(type: 0));

                      await Future.delayed(Duration(seconds: 2));
                      SmartDialog.dismiss();
                    },
                    child: Text('custom-smile')),
                ElevatedButton(
                    onPressed: () async {
                      SmartDialog.compatible.showLoading(
                          isLoadingTemp: false, widget: CustomLoading(type: 1));

                      await Future.delayed(Duration(seconds: 2));
                      SmartDialog.dismiss();
                    },
                    child: Text('custom-icon')),
                ElevatedButton(
                    onPressed: () async {
                      SmartDialog.compatible.showLoading(
                          isLoadingTemp: false, widget: CustomLoading(type: 2));

                      await Future.delayed(Duration(seconds: 2));
                      SmartDialog.dismiss();
                    },
                    child: Text('custom-normal')),
              ],
            ),
            Text('Dialog', textScaleFactor: 2),
            Wrap(
              spacing: 20,
              alignment: WrapAlignment.center,
              runSpacing: 20,
              runAlignment: WrapAlignment.center,
              children: [
                ElevatedButton(
                    onPressed: () {
                      var custom = Container(
                        height: 80,
                        width: 180,
                        decoration: BoxDecoration(
                          color: Colors.black,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        alignment: Alignment.center,
                        child: Text('easy custom dialog',
                            style: TextStyle(color: Colors.white)),
                      );
                      // here
                      SmartDialog.compatible.show(
                          widget: custom,
                          isLoadingTemp: false,
                          backDismiss: true);
                    },
                    child: Text('easy')),
                ElevatedButton(
                    onPressed: () async {
                      var stack = ({
                        double width = double.infinity,
                        double height = double.infinity,
                        String? msg,
                      }) {
                        return Container(
                          width: width,
                          height: height,
                          color: Colors.yellow,
                          alignment: Alignment.center,
                          child: Text('dialog $msg',
                              style: TextStyle(color: Colors.black)),
                        );
                      };

//left
                      SmartDialog.compatible.show(
                        tag: 'A',
                        widget: stack(msg: 'A', width: 70),
                        alignmentTemp: Alignment.centerLeft,
                      );
                      await Future.delayed(Duration(milliseconds: 500));
//top
                      SmartDialog.compatible.show(
                        tag: 'B',
                        widget: stack(msg: 'B', height: 70),
                        alignmentTemp: Alignment.topCenter,
                      );
                      await Future.delayed(Duration(milliseconds: 500));
//right
                      SmartDialog.compatible.show(
                        tag: 'C',
                        widget: stack(msg: 'C', width: 70),
                        alignmentTemp: Alignment.centerRight,
                      );
                      await Future.delayed(Duration(milliseconds: 500));
//bottom
                      SmartDialog.compatible.show(
                        tag: 'D',
                        widget: stack(msg: 'D', height: 70),
                        alignmentTemp: Alignment.bottomCenter,
                      );
                      await Future.delayed(Duration(milliseconds: 500));

//center：the stack handler
                      SmartDialog.compatible.show(
                        alignmentTemp: Alignment.center,
                        isLoadingTemp: false,
                        widget: Container(
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(15)),
                          padding: EdgeInsets.symmetric(
                              horizontal: 30, vertical: 20),
                          child: Wrap(spacing: 20, children: [
                            ElevatedButton(
                              child: Text('close dialog A'),
                              onPressed: () => SmartDialog.dismiss(tag: 'A'),
                            ),
                            ElevatedButton(
                              child: Text('close dialog B'),
                              onPressed: () => SmartDialog.dismiss(tag: 'B'),
                            ),
                            ElevatedButton(
                              child: Text('close dialog C'),
                              onPressed: () => SmartDialog.dismiss(tag: 'C'),
                            ),
                            ElevatedButton(
                              child: Text('close dialog D'),
                              onPressed: () => SmartDialog.dismiss(tag: 'D'),
                            ),
                          ]),
                        ),
                      );
                    },
                    child: Text('location')),
                ElevatedButton(
                  onPressed: () async {
                    SmartDialog.compatible.show(
                      alignmentTemp: Alignment.centerRight,
                      isPenetrateTemp: true,
                      clickBgDismissTemp: false,
                      widget: Container(
                        width: 80,
                        height: double.infinity,
                        color: Colors.cyan,
                      ),
                    );
                    await Future.delayed(Duration(seconds: 2));
                    SmartDialog.dismiss();
                  },
                  child: Text('penetrate'),
                ),
                ElevatedButton(
                  onPressed: () async {
                    VoidCallback callback = () {};

                    // display
                    SmartDialog.compatible.show(
                      alignmentTemp: Alignment.center,
                      widget: OtherTrick(
                        onUpdate: (VoidCallback onInvoke) =>
                            callback = onInvoke,
                      ),
                    );

                    await Future.delayed(Duration(milliseconds: 200));

                    // handler
                    SmartDialog.compatible.show(
                      alignmentTemp: Alignment.bottomCenter,
                      maskColorTemp: Colors.transparent,
                      widget: Container(
                        height: 150,
                        width: double.infinity,
                        color: Colors.white,
                        alignment: Alignment.center,
                        child: ElevatedButton(
                          child: Text('add'),
                          onPressed: () => callback.call(),
                        ),
                      ),
                    );
                  },
                  child: Text('dialogStack'),
                ),
              ],
            ),
          ],
        ));
  }
}

class OtherTrick extends StatefulWidget {
  const OtherTrick({Key? key, required this.onUpdate}) : super(key: key);

  final Function(VoidCallback onInvoke) onUpdate;

  @override
  _OtherTrickState createState() => _OtherTrickState();
}

class _OtherTrickState extends State<OtherTrick> {
  int _count = 0;

  @override
  void initState() {
    // here
    widget.onUpdate.call(() {
      _count++;
      setState(() {});
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(10),
        ),
        child: Text('Counter: $_count',
            style: TextStyle(fontSize: 30.0, color: Colors.white)),
      ),
    );
  }
}
